<?php
class Carts extends MX_Controller
{
    function __construct()
    {
        parent::__construct();                     
    }
    function index($id=0)
    {                
        $this->common->load_template('carts_index',$data);
    }
    function cart()
    {                
        foreach ($this->cart->contents() as $items)
        {
            if ($this->input->post($items['rowid']) !='')
            {
                $row_qty = $this->input->post($items['rowid']);
                $data = array(
                    'rowid'=>$items['rowid'],               
                    'qty'     => $row_qty,                                
                );
                $this->cart->update($data);
            }                
        }
        $data['curent'] = 0;    
        $data['flag'] = 1;

        $this->common->load_template('carts_cart',$data);
    }
    function delete_cart()
    {
        $rowid = $this->input->post('rowid');
        $data = array(
                'rowid'=>$rowid,               
               'qty'     => 0,
                            
            );        
        $this->cart->update($data);
        $total_items = $this->cart->total_items().' item';            
        $tong_tien = $this->cart->format_number($this->cart->total());                        
        $data['tong_tien'] = $tong_tien;
        $data['total_items'] = $total_items;
        $data = json_encode($data);
        echo $data;        
    }
    function check_exist_cart($id)
    {
        foreach ($this->cart->contents() as $item)
        {
            if ($item['id'] == $id ) { return $item; } ;
        }
        return 0;
    }
    function add_cart()
    {
        $id = $this->input->post('id');           
        $product = new Product();
        $product =  $product->get_by_id($id);
        $item = $this->check_exist_cart($id);        
        if ( $item == 0)
        {               
            $data = array(            
                   'id'      => $id,
                   'qty'     => 1,
                   'price'   => $product->price,
                   'name'    => $this->common->get_lang($product->name),               
                );
            $rowid = $this->cart->insert($data);       
            $html = '';
            $html .= '<tr class="'.$rowid.'">';

            $html .=   '<td>'.$this->common->get_lang($product->name);		
            $html .=   '</td>';
            $html .=   '<td id="qty_'.$rowid.'" class="col_qty"> <input type="text" size="1" maxlength="3" value="1" name="'.$rowid.'"></td>';;
            $html .=   '<td class="col_price" style="text-align:right">'.number_format($product->price).' Đ</td>';    	
            $html .=   '<td><div class="delete_cart" rowid="'.$rowid.'"><img src="'.base_url().'content/images/delete_16x16.gif"/></div></td>';
            $html .=    '</tr>';
            
            $total_items = $this->cart->total_items().' item';            
            $tong_tien = $this->cart->format_number($this->cart->total());            
            
            $data['tong_tien'] = $tong_tien;
            $data['total_items'] = $total_items;
            $data['html'] = $html;
            $data['rowid'] = 0;
            $data = json_encode($data);
            echo $data;
        } else
        {
            $item['qty'] = $item['qty']+1;            
            $this->cart->update($item);
            $tong_tien = $this->cart->format_number($this->cart->total()); 
            $total_items = $this->cart->total_items().' item';
            $data['tong_tien'] = $tong_tien;
            $data['total_items'] = $total_items;
            $data['html'] = ' <input type="text" size="1" maxlength="3" value="'.$item['qty'].'" name="'.$item['rowid'].'">';
            $data['rowid'] = $item['rowid'];
            $data = json_encode($data);
            echo $data;
        }        
    }
}
?>